/*!
 * 界面入口模块  
 */

layui.extend({
    setter: 'config' //配置文件
    ,
    admin: 'lib/admin' //核心模块
    ,
    view: 'lib/view' //核心模块
    ,
    main: 'lib/main',
    tableSelect: 'lib/tableSelect',
    treeSelect: 'lib/extend/treeSelect',
    yjUrl: 'lib/yjUrl',
    platform: 'views/platform/platform'
}).define(['setter', 'admin', 'main', 'yjUrl', 'tableSelect','treeSelect'], function (exports) {
    var setter = layui.setter,
        element = layui.element,
        admin = layui.admin,
        yjUrl = layui.yjUrl,
        main = layui.main,
        tableSelect = layui.tableSelect,
        treeSelect = layui.treeSelect,
        tabsPage = admin.tabsPage,
        view = layui.view


        , openTabsPage = function (url, text) {
            //遍历页签选项卡
            var matchTo, tabs = $('#LAY_app_tabsheader>li'),
                path = url.replace(/(^http(s*):)|(\?[\s\S]*$)/g, '');

            tabs.each(function (index) {
                var li = $(this),
                    layid = li.attr('lay-id');

                if (layid === url) {
                    matchTo = true;
                    tabsPage.index = index;
                }
            });
            text = text || '新标签页';

            //定位当前tabs
            var setThisTab = function () {
                element.tabChange(FILTER_TAB_TBAS, url);
                admin.tabsBodyChange(tabsPage.index, {
                    url: url,
                    text: text
                });
            };
            if (setter.pageTabs) {
                //如果未在选项卡中匹配到，则追加选项卡
                if (!matchTo) {
                    //延迟修复 Firefox 空白问题
                    setTimeout(function () {
                        $(APP_BODY).append([
                            '<div class="layadmin-tabsbody-item layui-show">', '<iframe src="/src/views/' + url + '" frameborder="0" class="layadmin-iframe"></iframe>', '</div>'
                        ].join(''));
                        setThisTab();
                    }, 10);

                    tabsPage.index = tabs.length;
                    element.tabAdd(FILTER_TAB_TBAS, {
                        title: '<span>' + text + '</span>',
                        id: url,
                        attr: path
                    });

                }
            } else {
                var iframe = admin.tabsBody(admin.tabsPage.index).find('.layadmin-iframe');
                iframe[0].contentWindow.location.href = url;
            }

            setThisTab();
        }

        //入口页面
        , entryPage = function (fn) {
            var container = view(setter.container);
            var localplatform = layui.sessionData(setter.tableName + 'platform');
            var local = layui.sessionData(setter.tableName);
            if (!local[setter.request.tokenName]) {
                container.render('login/index').done(function () {
                    layui.element.render();
                });
            } else {
                if (localplatform["platform"]) {
                    container.render('layout').done(function () {
                        layui.element.render();
                    });
                } else {     
                    container.render('platform/platform').done(function () {
                        layui.element.render();
                    });
                }
            }
        }


        , APP_BODY = '#LAY_app_body', FILTER_TAB_TBAS = 'layadmin-layout-tabs', $ = layui.$, $win = $(window);

    admin.setReqHeader();
    //对外输出
    exports('index', {
        openTabsPage: openTabsPage,
        entryPage: entryPage
    });
});